package com.wing.product.controller.admin;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.product.model.entity.ProductType;
import com.wing.product.model.response.ProductTypeVO;
import com.wing.product.service.ProductTypeService;
import com.wing.web.utils.JwtUtils;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.PUT;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 存放产品类型
 * @author xh
 */
@Validated
@Api(value = "Admin-存放产品类型", tags = "#/productCab/storeProductType")
@RestController("StoreProductType")
@RequestMapping("cabProduct/admin/storeProductType")
@RequiredArgsConstructor
@Slf4j
public class StoreProductTypeController {



    @Autowired
    private ProductTypeService productTypeService;

    /**
     * 查询 为否的智能柜刀具类型
     * @param name  类型名称
     * @param type 标记 is 列表 no添加列表
     */
    @GetMapping("/getCabCuttertypeList")
    public PageResult<ProductType> getCabCuttertypeList(
            @RequestParam(defaultValue = "1") Integer page
            , @RequestParam(defaultValue = "10") Integer limit
            , @RequestParam(defaultValue = "create_time") String sort
            , @RequestParam(defaultValue = "asc") String order
            , String name
            ,String type
    ){
        Page<ProductType> pg = new Page<>(page, limit);

        QueryWrapper<ProductType>queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");

        if (StringUtils.isNotBlank(type)){
            if ("is".equals(type)){
                queryWrapper.eq("cab_cutter_type",1);
            }else {
                queryWrapper.eq("cab_cutter_type",0);
            }
        }


        if (StringUtils.isNotBlank(name)){
            queryWrapper.eq("name",name);
        }
        IPage<ProductType> list = productTypeService.getBaseMapper().selectPage(pg,queryWrapper);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    /**
     * 添加到智能柜存放物资类型 操作
     * @param productType
     * @return
     */
    @PutMapping("/upProductType")
    public JsonResult upProductType(ProductType productType){
        try {
            productType.setCabCutterType(true);
            if (productTypeService.updateById(productType)){
                return JsonResult.success("操作成功");
            }
        }catch (Exception e){
            log.error("upProductType 操作失败"+e);
        }
        return JsonResult.fail("操作失败");
    }

    /**
     * 设置
     */
    @PutMapping("/upDate")
    public JsonResult upDate(ProductType productType){
        try {
            productType.setModifierName(JwtUtils.getLoginUsername());
            productType.setUpdateTime(LocalDateTime.now());
            if (productTypeService.updateById(productType)){
                return JsonResult.success("操作成功");
            }
        }catch (Exception e){
            log.error("upProductType 操作失败"+e);
        }
        return JsonResult.fail("操作失败");
    }


}
